import os
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.middleware.cors import CORSMiddleware


# fastapi app
app = FastAPI(docs_url=None, redoc_url=None, title="Drink-Making")

# response gzip压缩
app.add_middleware(GZipMiddleware, minimum_size=500)

# 跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_headers=["*"],
)

def run_web_server(ip='0.0.0.0', port=80, app_title='FastApi', dev_mode=True):
    # 开发者模式打印详细log
    app.title = app_title
    if not dev_mode:
        log_config = uvicorn.config.LOGGING_CONFIG
        log_config["loggers"] = {
            "uvicorn": {"handlers": ["default"], "level": "ERROR", "propagate": False},
            "uvicorn.error": {"level": "ERROR"},
            "uvicorn.access": {"handlers": ["access"], "level": "ERROR", "propagate": False},
        }
        uvicorn.run(app=app, host=ip, port=port, reload=False, loop="asyncio", log_config=log_config, lifespan="on")
    else:
        uvicorn.run(app=app, host=ip, port=port, reload=False, loop="asyncio", lifespan="on")